package com.wd.live.wxapi;

import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;


import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wd.live.R;
import com.wd.live.base.BaseActivity;
import com.wd.live.bean.WxBean;
import com.wd.live.databinding.WxItemBinding;
import com.wd.live.model.SpUtils;
import com.wd.live.model.utils.ToastFormat;
import com.wd.live.viewmodel.WxModel;


public class WXEntryActivity extends BaseActivity<WxModel, WxItemBinding> implements IWXAPIEventHandler {
    private IWXAPI iwxapi;
    private static String APP_ID = "wx064904aa8809f5aa";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //接收到分享以及登录的intent传递handleIntent方法，处理结果
        iwxapi = WXAPIFactory.createWXAPI(this, APP_ID, false);
        iwxapi.handleIntent(getIntent(), this);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected int initLayout() {
        return R.layout.wx_item;
    }


    @Override
    public void onReq(BaseReq baseReq) {
        Log.e("onReq", "不是请求错误");//我们一般用不到
    }

    @Override
    public void onResp(BaseResp baseResp) {
        //登录回调
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK://代表成功

                //获取code临时票据
                String code = ((SendAuth.Resp) baseResp).code;
                Log.i("TAG", "code："+ code);
                SpUtils.putString("code",code);
                //请求后台封装好的微信登录接口，获取登录后的json数据
                //请求操作省略……
            viewmodel.getWxData(code,"application/x-www-form-urlencoded").observe(this, new Observer<WxBean>() {
                @Override
                public void onChanged(WxBean wxBeen) {
                    ToastFormat.setText(wxBeen.getMessage());
                    ToastFormat.show();
                    if(wxBeen!=null&&wxBeen.getStatus().equals("0000")){
                        ToastFormat.setText("微信登录成功");
                        ToastFormat.show();
                        SpUtils.putBoolean("islogin",true);
                        SpUtils.putString("name",wxBeen.getResult().getNickName());
                        SpUtils.putString("pic",wxBeen.getResult().getHeadPic());
                        SpUtils.putString("uid",wxBeen.getResult().getUserId()+"");
                        SpUtils.putString("sid",wxBeen.getResult().getSessionId());
                        Log.v("userId",wxBeen.getResult().getUserId()+"");
                        Log.v("sessionId",wxBeen.getResult().getSessionId());
                        finish();
                    }
                }
            });

                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED://用户拒绝授权
                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL://用户取消
                finish();
                break;
            default:
                finish();
                break;
        }
    }

    @Override
    public void onChanged(Object o) {

    }
}
